//
//  BRSMessageListViewController.m
//  Pint
//
//  Created by xiect on 13-11-1.
//  Copyright (c) 2013年 brains. All rights reserved.
//

#import "JTRevealSidebarV2Delegate.h"
#import "UIViewController+JTRevealSidebarV2.h"
#import "UINavigationItem+JTRevealSidebarV2.h"

#import "BRSMenuViewController.h"
#import "BRSMessageListViewController.h"

@interface BRSMessageListViewController (Private) <UITableViewDataSource, UITableViewDelegate, BRSMenuViewControllerDelegate>

@end

@implementation BRSMessageListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navi_menu"]  style:UIBarButtonItemStyleBordered target:self action:@selector(showMenuView:)];
    
     self.navigationItem.revealSidebarDelegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// slid left menu
- (void)showMenuView:(id)sender {
    [self.navigationController toggleRevealState:JTRevealedStateLeft];
}

#pragma mark JTRevealSidebarDelegate
// This is an examle to configure your sidebar view through a custom UIViewController
- (UIView *)viewForLeftSidebar {
    CGRect viewFrame = self.navigationController.applicationViewFrame;
    UITableViewController *controller = self.leftMenuViewController;
    if ( ! controller) {
        self.leftMenuViewController = [[BRSMenuViewController  alloc] init];
        self.leftMenuViewController.menuViewDelegate = self;
        controller = self.leftMenuViewController;
        controller.title = @"menuview";
    }
    controller.view.frame = CGRectMake(0, viewFrame.origin.y, kSLIDE_MENU_WIDTH, viewFrame.size.height);
    controller.view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
    return controller.view;
}

// Optional delegate methods for additional configuration after reveal state changed
- (void)didChangeRevealedStateForViewController:(UIViewController *)viewController {
    // Example to disable userInteraction on content view while sidebar is revealing
    if (viewController.revealedState == JTRevealedStateNo) {
        self.view.userInteractionEnabled = YES;
    } else {
        self.view.userInteractionEnabled = NO;
    }
}

#pragma mark SidebarViewControllerDelegate

- (void)menuViewController:(BRSMenuViewController *)menuViewController didSelectObject:(NSObject *)object atIndexPath:(NSIndexPath *)indexPath {
    
    [self.navigationController setRevealedState:JTRevealedStateNo];
    
    BRSMessageListViewController *controller = [[BRSMessageListViewController alloc] init];
    controller.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
    controller.title = (NSString *)object;
    controller.leftMenuViewController = menuViewController;
    controller.leftMenuSelectedIndexPath      = indexPath;
    menuViewController.menuViewDelegate = controller;
    [self.navigationController setViewControllers:[NSArray arrayWithObject:controller] animated:NO];
    
}

- (NSIndexPath *)lastSelectedIndexPathForMenuViewController:(BRSMenuViewController *)menuViewController {
    return self.leftMenuSelectedIndexPath;
}
@end
